/**
 * 
 */
package com.nari.baseinfo.action;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.IBuilding;
import com.nari.baseinfo.model.ICommunity;
import com.nari.baseinfo.model.IHeatRoom;
import com.nari.baseinfo.model.IProject;
import com.nari.baseinfo.model.IShareArea;
import com.nari.baseinfo.service.IBaseStatisService;
import com.nari.baseinfo.service.IBuildingService;
import com.nari.baseinfo.service.ICommunityService;
import com.nari.baseinfo.service.IHeatRoomService;
import com.nari.baseinfo.service.IProjectService;
import com.nari.baseinfo.service.IShareAreaService;
import com.nari.baseinfo.utils.BaseStatisConstants;
import com.nari.baseinfo.utils.statisentity.IGenericStatisEntity;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-1
 */
public class ShareAreaAction extends JSONActionSupport {
	private static final long serialVersionUID = 9069599158941700185L;
	private IShareAreaService iShareAreaService;
	private IProjectService iProjectService;
	private ICommunityService iCommunityService;
	private IBuildingService iBuildingService;
	private IHeatRoomService iHeatRoomService;
	private IBaseStatisService iBaseStatisService;
	// 分摊域信息查询
	private String areaName;
	private String projectUID;
	private String communityUID;
	private List<ICommunity> communityList;
	private List<IProject> projectList;
	private Map<String, String> pageProjectMap; // 所属项目id-projectName映射表
	private Map<String, String> pageCommunityMap; // 所属小区id-communityName映射表
	private List<IBuilding> buildingList;
	// 分页查询
	private int pageIndex = 1; // 默认查询第一页列表
	private PageCond pageCond;
	private int pageSize;
	private ResultWithPageCond<IShareArea> shareAreaPage;
	// 分摊域信息管理
	private String shareAreaUID;
	private IShareArea shareArea;
	// 项目所属分摊域查询
	private List<IShareArea> shareAreaList;
	//分摊域详细资料查询
	private IProject project;
	private ICommunity community;
	private List<IHeatRoom> roomList;
	private Map<String, String> pageBuildingMap;
	private IGenericStatisEntity statisEntity;
	
	/**
	 * 分摊域信息管理导航
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-26
	 */
	public String queryShareAreaMgrNav() throws Exception {
		//便于系统参数携带
		return "shareAreaMgrNav";
	}

	/**
	 * 分页查询分摊域列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-7
	 */
	public String queryShareAreaPage() throws Exception {
		// 所属项目列表信息
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if (this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		// 分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		// 分页结果
		this.shareAreaPage = this.iShareAreaService.queryShareAreaWithPagecond(
				areaName, projectUID, communityUID, pageCond);
		// 计算结果集的页数
		this.pageSize = new BigDecimal(this.shareAreaPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.shareAreaPage.getPageCond().getLimit()), RoundingMode.UP)
				.intValue();
		if (pageSize == 0) pageSize = 1;
		// 所属项目映射表
		this.pageProjectMap = new HashMap<String, String>();
		for (IProject project : projectList) {
			pageProjectMap.put(project.getId(), project.getProjectName());
		}
		// 所属小区映射表
		this.pageCommunityMap = this.iShareAreaService
				.queryReferCommunitysByAreaList(shareAreaPage.getList());
		return "shareAreaList";
	}

	/**
	 * 分摊域信息管理
	 * 
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-7
	 */
	public String queryShareAreaForManage() throws Exception {
		// 分摊域信息
		if (this.shareAreaUID != null && !"".equals(shareAreaUID.trim())) {
			this.shareArea = this.iShareAreaService.queryEntityById(shareAreaUID.trim());
			this.projectUID = this.shareArea.getProjectID();
			this.communityUID = this.shareArea.getCommunityID();
		}
		// 所属项目列表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if (this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		// 所属小区列表
		if (projectUID != null && !"".equals(projectUID)) {
			this.communityList = this.iCommunityService.queryCommunitysByBelongings(null, null, null, projectUID);
		} else {
			this.communityList = new ArrayList<ICommunity>();
		}
		return "shareAreaManage";
	}

	/**
	 * 新增或修改分摊域信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-8
	 */
	public String saveOrUpdateShareArea() throws Exception {
		// 二步制比例-百分数转化为小数
		double binaryPercent = new BigDecimal(shareArea.getBinaryPercent())
				.divide(new BigDecimal(100.0), 5, RoundingMode.HALF_UP)
				.doubleValue();
		// 基本热价(基本热价=二步制比例×面积热价)
		double basePrice = new BigDecimal(binaryPercent)
				.multiply(new BigDecimal(shareArea.getAreaChargePrice()), new MathContext(5, RoundingMode.HALF_UP))
				.doubleValue();
		this.shareArea.setBasePrice(basePrice);
		// 保存分摊域基本信息
		if (this.shareArea.getId() != null && !"".equals(shareArea.getId())) {
			this.iShareAreaService.updateEntity(shareArea, shareArea.getId());
		} else {
			this.iShareAreaService.saveEntity(shareArea);
		}
		this.writeJsonStrToResponse(JSONUtil.serialize(ResponseMsg.getInstance(true, "")));
		return Action.SUCCESS;
	}

	/**
	 * 通过唯一标识删除分摊域
	 * 
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-8
	 */
	public String deleteShareAreaByUID() throws Exception {
		this.rmsg = this.iShareAreaService.deleteCascadeByAreaUID(shareAreaUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}

	/**
	 * 查询项目所属分摊域列表
	 * 
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-20
	 */
	public String queryShareAreasByBelongings() throws Exception {
		this.shareAreaList = this.iShareAreaService.queryShareAreaByBelongings(projectUID, communityUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(shareAreaList));
		return Action.SUCCESS;
	}

	/**
	 * 分摊域关联信息管理初始化
	 * 
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-4
	 */
	public String initialShareAreaLinkMgr() throws Exception {
		// 初始化分摊域ID标识(shareAreaUID)
		return "initAreaLink";
	}

	/**
	 * 通过唯一标识查询分摊域实体
	 * 
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-4
	 */
	public String queryShareAreaEntityByUID() throws Exception {
		this.shareArea = this.iShareAreaService.queryEntityById(shareAreaUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(this.shareArea));
		return Action.SUCCESS;
	}

	/**
	 * 分摊域信息查看
	 * @return
	 * @throws Exception
	 * @author yw
	 * @date 2013-9-15
	 */
	public String showShareAreaDetail() throws Exception {
		// 分摊域信息
		if (this.shareAreaUID != null && !"".equals(shareAreaUID)) {
			this.shareArea = this.iShareAreaService.queryEntityById(shareAreaUID);
			//所属项目
			this.project = iProjectService.queryEntityById(shareArea.getProjectID());
			//所属小区
			this.community = iCommunityService.queryEntityById(shareArea.getCommunityID());
			//统计信息
			this.statisEntity = iBaseStatisService.queryBaseStatisEntity(BaseStatisConstants.statisType_shareAreaDetail, shareAreaUID);
			//所属热用户
			this.roomList = iHeatRoomService.queryHeatRoomsByBelongs(null, null, shareAreaUID, null);
			// 楼栋映射表
			this.pageBuildingMap = this.iHeatRoomService.queryReferBuildingsByRoomList(roomList);
		}
		return "shareAreaDetail";
	}

	/**
	 * @return the iShareAreaService
	 */
	@JSON(serialize = false)
	public IShareAreaService getiShareAreaService() {
		return iShareAreaService;
	}

	/**
	 * @param iShareAreaService
	 *            the iShareAreaService to set
	 */
	public void setiShareAreaService(IShareAreaService iShareAreaService) {
		this.iShareAreaService = iShareAreaService;
	}

	/**
	 * @return the iProjectService
	 */
	@JSON(serialize = false)
	public IProjectService getiProjectService() {
		return iProjectService;
	}

	/**
	 * @param iProjectService
	 *            the iProjectService to set
	 */
	public void setiProjectService(IProjectService iProjectService) {
		this.iProjectService = iProjectService;
	}

	/**
	 * @return the iCommunityService
	 */
	@JSON(serialize = false)
	public ICommunityService getiCommunityService() {
		return iCommunityService;
	}

	/**
	 * @param iCommunityService
	 *            the iCommunityService to set
	 */
	public void setiCommunityService(ICommunityService iCommunityService) {
		this.iCommunityService = iCommunityService;
	}

	/**
	 * @return the iBuildingService
	 */
	@JSON(serialize = false)
	public IBuildingService getiBuildingService() {
		return iBuildingService;
	}

	/**
	 * @param iBuildingService the iBuildingService to set
	 */
	public void setiBuildingService(IBuildingService iBuildingService) {
		this.iBuildingService = iBuildingService;
	}

	/**
	 * @return the iHeatRoomService
	 */
	@JSON(serialize = false)
	public IHeatRoomService getiHeatRoomService() {
		return iHeatRoomService;
	}

	/**
	 * @param iHeatRoomService the iHeatRoomService to set
	 */
	public void setiHeatRoomService(IHeatRoomService iHeatRoomService) {
		this.iHeatRoomService = iHeatRoomService;
	}

	/**
	 * @return the iBaseStatisService
	 */
	@JSON(serialize = false)
	public IBaseStatisService getiBaseStatisService() {
		return iBaseStatisService;
	}

	/**
	 * @param iBaseStatisService the iBaseStatisService to set
	 */
	public void setiBaseStatisService(IBaseStatisService iBaseStatisService) {
		this.iBaseStatisService = iBaseStatisService;
	}

	/**
	 * @return the areaName
	 */
	public String getAreaName() {
		return areaName;
	}

	/**
	 * @param areaName
	 *            the areaName to set
	 */
	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}

	/**
	 * @return the projectUID
	 */
	public String getProjectUID() {
		return projectUID;
	}

	/**
	 * @param projectUID
	 *            the projectUID to set
	 */
	public void setProjectUID(String projectUID) {
		this.projectUID = projectUID;
	}

	/**
	 * @return the communityUID
	 */
	public String getCommunityUID() {
		return communityUID;
	}

	/**
	 * @param communityUID
	 *            the communityUID to set
	 */
	public void setCommunityUID(String communityUID) {
		this.communityUID = communityUID;
	}

	/**
	 * @return the communityList
	 */
	public List<ICommunity> getCommunityList() {
		return communityList;
	}

	/**
	 * @param communityList
	 *            the communityList to set
	 */
	public void setCommunityList(List<ICommunity> communityList) {
		this.communityList = communityList;
	}

	/**
	 * @return the projectList
	 */
	public List<IProject> getProjectList() {
		return projectList;
	}

	/**
	 * @param projectList
	 *            the projectList to set
	 */
	public void setProjectList(List<IProject> projectList) {
		this.projectList = projectList;
	}

	/**
	 * @return the pageProjectMap
	 */
	public Map<String, String> getPageProjectMap() {
		return pageProjectMap;
	}

	/**
	 * @param pageProjectMap
	 *            the pageProjectMap to set
	 */
	public void setPageProjectMap(Map<String, String> pageProjectMap) {
		this.pageProjectMap = pageProjectMap;
	}

	/**
	 * @return the pageCommunityMap
	 */
	public Map<String, String> getPageCommunityMap() {
		return pageCommunityMap;
	}

	/**
	 * @param pageCommunityMap
	 *            the pageCommunityMap to set
	 */
	public void setPageCommunityMap(Map<String, String> pageCommunityMap) {
		this.pageCommunityMap = pageCommunityMap;
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex
	 *            the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the pageCond
	 */
	public PageCond getPageCond() {
		return pageCond;
	}

	/**
	 * @param pageCond
	 *            the pageCond to set
	 */
	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize
	 *            the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the shareAreaPage
	 */
	public ResultWithPageCond<IShareArea> getShareAreaPage() {
		return shareAreaPage;
	}

	/**
	 * @param shareAreaPage
	 *            the shareAreaPage to set
	 */
	public void setShareAreaPage(ResultWithPageCond<IShareArea> shareAreaPage) {
		this.shareAreaPage = shareAreaPage;
	}

	/**
	 * @return the shareAreaUID
	 */
	public String getShareAreaUID() {
		return shareAreaUID;
	}

	/**
	 * @param shareAreaUID
	 *            the shareAreaUID to set
	 */
	public void setShareAreaUID(String shareAreaUID) {
		this.shareAreaUID = shareAreaUID;
	}

	/**
	 * @return the shareArea
	 */
	public IShareArea getShareArea() {
		return shareArea;
	}

	/**
	 * @param shareArea
	 *            the shareArea to set
	 */
	public void setShareArea(IShareArea shareArea) {
		this.shareArea = shareArea;
	}

	/**
	 * @return the shareAreaList
	 */
	public List<IShareArea> getShareAreaList() {
		return shareAreaList;
	}

	/**
	 * @param shareAreaList
	 *            the shareAreaList to set
	 */
	public void setShareAreaList(List<IShareArea> shareAreaList) {
		this.shareAreaList = shareAreaList;
	}

	public List<IBuilding> getBuildingList() {
		return buildingList;
	}

	public void setBuildingList(List<IBuilding> buildingList) {
		this.buildingList = buildingList;
	}

	/**
	 * @return the project
	 */
	public IProject getProject() {
		return project;
	}

	/**
	 * @param project the project to set
	 */
	public void setProject(IProject project) {
		this.project = project;
	}

	/**
	 * @return the community
	 */
	public ICommunity getCommunity() {
		return community;
	}

	/**
	 * @param community the community to set
	 */
	public void setCommunity(ICommunity community) {
		this.community = community;
	}

	/**
	 * @return the roomList
	 */
	public List<IHeatRoom> getRoomList() {
		return roomList;
	}

	/**
	 * @param roomList the roomList to set
	 */
	public void setRoomList(List<IHeatRoom> roomList) {
		this.roomList = roomList;
	}

	/**
	 * @return the pageBuildingMap
	 */
	public Map<String, String> getPageBuildingMap() {
		return pageBuildingMap;
	}

	/**
	 * @param pageBuildingMap the pageBuildingMap to set
	 */
	public void setPageBuildingMap(Map<String, String> pageBuildingMap) {
		this.pageBuildingMap = pageBuildingMap;
	}

	/**
	 * @return the statisEntity
	 */
	public IGenericStatisEntity getStatisEntity() {
		return statisEntity;
	}

	/**
	 * @param statisEntity the statisEntity to set
	 */
	public void setStatisEntity(IGenericStatisEntity statisEntity) {
		this.statisEntity = statisEntity;
	}

}
